package com.af.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.szy.system.api.result.Result;
import com.szy.system.util.ServletUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author : zhenyun.su
 * @comment : 按json格式返回http响应，
 * 参考：spring的DefaultRedirectStrategy(返回页面) 来开发
 * 使用说明：
 *     @Autowired
 *     private ObjectMapper objectMapper;
 *     @Bean
 *     public RedirectService redirectService(){
 *         return new RedirectService(objectMapper);
 *     }
 * @since : 2020/5/9
 */

public class AfRedirectService {

    private ObjectMapper objectMapper;

    public AfRedirectService(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    public  void redirectData(HttpServletResponse response, String context, String contentTyp) throws
            IOException {
        ServletUtils.redirectData(response, context, contentTyp);
    }

    public void redirectJson(HttpServletResponse response, Object result) throws IOException {
        String context = objectMapper.writeValueAsString(result);
        ServletUtils.redirectJson(response, context);
    }

    public void redirectJson(HttpServletResponse response, Result result) throws IOException {
        String context = objectMapper.writeValueAsString(result);
        ServletUtils.redirectJson(response, context);
    }

    public void redirectPage(HttpServletRequest request, HttpServletResponse response, String redirectUrl) throws IOException {
        ServletUtils.redirectPage(request, response, redirectUrl);
    }
}

